package com.eastfair.builder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.builder.dto.ReportSpaceDTO;
import com.eastfair.builder.dto.ReportSpacePageQuery;
import com.eastfair.builder.dto.ReportSpaceSaveDTO;
import com.eastfair.builder.dto.ReportSpaceUpdateDTO;
import com.eastfair.builder.entity.ReportSpace;
import com.eastfair.builder.service.ReportSpaceService;
import com.eastfair.builder.vo.ExhibitorVO;
import com.eastfair.builder.vo.ReportSpaceVO;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.home.dto.ReportSpaceAuditDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 报馆记录表
 * </p>
 *
 * @author dq
 * @date 2022-08-06
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/reportSpace")
@Api(value = "ReportSpace", tags = "报馆记录表")
@PreAuth(replace = "builder:reportSpace:")
public class ReportSpaceController extends SuperController<ReportSpaceService, Long, ReportSpace, ReportSpacePageQuery, ReportSpaceSaveDTO, ReportSpaceUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ReportSpaceService reportSpaceService;

    @Override
    public void handlerResult(IPage<ReportSpace> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ReportSpace> reportSpaceList = list
                .stream()
                .map((map) -> ReportSpace.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(reportSpaceList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ReportSpacePageQuery> params) {
        ReportSpacePageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 分页报馆记录
     *
     * @param pageParams the page params
     * @return the r
     */
    @ApiOperation(value = "分页报馆记录")
    @PostMapping("/query/reportSpace")
    public R<IPage<ReportSpaceVO>> queryReportSpace(@RequestBody PageParams<ReportSpaceDTO> pageParams) {
        log.info("queryReportSpace - 分页报馆记录, pageParams={}", pageParams);
        return R.successDef(reportSpaceService.listReportSpaceVOs(pageParams));
    }

    /**
     * ID获取报馆详情.
     *
     * @param id the id
     * @return the by id
     */
    @ApiOperation(value = "ID获取报馆详情")
    @GetMapping("/get/{id}")
    public R<ReportSpaceVO> getById(@PathVariable("id") Long id) {
        log.info("getById - ID获取报馆详情, id={}", id);
        return R.successDef(reportSpaceService.getReportSpaceVOById(id));
    }

    /**
     * 展商ID查询报馆记录.
     *
     * @param exhibitorId the exhibitor id
     * @return the by exhibitor id
     */
    @ApiOperation(value = "展商ID查询报馆记录")
    @GetMapping("/get/exhibitor/{exhibitorId}")
    public R<ReportSpace> getByExhibitorId(@PathVariable("exhibitorId") Long exhibitorId) {
        log.info("getByExhibitorId - 展商ID查询报馆记录, exhibitorId={}", exhibitorId);
        try {
            return R.successDef(reportSpaceService.getByExhibitorId(exhibitorId));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 报馆id查询展位保证金.
     *
     * @param id the id
     * @return the site bond by id
     */
    @GetMapping("/get/shouldEarnestMoney/{id}")
    public R<ExhibitorVO> getShouldEarnestMoneyById(@PathVariable("id") Long id) {
        log.info("getSiteBondById - 报馆id查询展位保证金, id={}", id);
        return R.successDef(reportSpaceService.getShouldEarnestMoneyById(id));
    }

    /**
     * 保存报馆记录.
     *
     * @param reportSpaceDTO the report space dto
     * @return the r
     */
    @ApiOperation(value = "保存报馆记录")
    @SysLog("添加报馆记录")
    @PostMapping("/save")
    public R<ReportSpace> saveReportSpace(@RequestBody ReportSpaceDTO reportSpaceDTO) {
        log.info("saveReportSpace - 保存报馆记录, reportSpaceDTO={}", reportSpaceDTO);
        return R.successDef(reportSpaceService.saveReportSpace(reportSpaceDTO));
    }

    /**
     * 提交报馆记录.
     *
     * @param reportSpaceId the report space id
     * @return the r
     */
    @ApiOperation(value = "提交报馆记录")
    @SysLog("提交报馆记录")
    @GetMapping("/submit/{reportSpaceId}")
    public R<Boolean> submitReportSpace(@PathVariable("reportSpaceId") Long reportSpaceId) {
        log.info("submitReportSpace - 提交报馆记录, reportSpaceId={}", reportSpaceId);
        return R.successDef(reportSpaceService.submitReportSpace(reportSpaceId));
    }

    /**
     * 取消提交报馆记录.
     *
     * @param reportSpaceId the report space id
     * @return the r
     */
    @ApiOperation(value = "提交报馆记录")
    @SysLog("取消报馆")
    @GetMapping("/cancel/submit/{reportSpaceId}")
    public R<Boolean> cancelSubmitReportSpace(@PathVariable("reportSpaceId") Long reportSpaceId) {
        log.info("cancelSubmitReportSpace - 取消提交报馆记录, reportSpaceId={}", reportSpaceId);
        return R.successDef(reportSpaceService.updateReportSpaceAuditStatusNotSubmit(reportSpaceId));
    }

    /**
     * 更新报馆审核状态.
     *
     * @param reportSpaceDTO the report space dto
     * @return the r
     */
    @ApiOperation(value = "更新报馆审核状态")
    @SysLog("修改报馆")
    @PostMapping("/update/auditStatus")
    public R<Boolean> updateAuditStatus(@RequestBody ReportSpaceDTO reportSpaceDTO) {
        log.info("updateAuditStatus - 更新报馆审核状态, reportSpaceDTO={}", reportSpaceDTO);
        try {
            return R.successDef(reportSpaceService.updateReportSpaceAuditStatus(reportSpaceDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 更新场馆审核状态.
     *
     * @param reportSpaceDTO the report space dto
     * @return the r
     */
    @PostMapping("/update/venue/auditStatus")
    public R<Boolean> updateVenueAuditStatus(@RequestBody ReportSpaceDTO reportSpaceDTO) {
        log.info("updateVenueAuditStatus - 更新场馆审核状态, reportSpaceDTO={}", reportSpaceDTO);
        try {
            return R.successDef(reportSpaceService.updateVenueAuditStatus(reportSpaceDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 发送图纸审核通过信息
     *
     * @param reportSpaceId the report space id
     * @return the r
     */
    @GetMapping("/send/drawingAudit/msg/{reportSpaceId}")
    public R<Boolean> sendDrawingAuditMsg(@PathVariable("reportSpaceId") Long reportSpaceId) {
        log.info("sendDrawingAuditMsg - 发送图纸审核通过信息, reportSpaceId={}", reportSpaceId);
        try {
            return R.successDef(reportSpaceService.sendDrawingAuditMsg(reportSpaceId));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 发送图纸审核通过信息
     *
     * @param reportSpaceAuditDTO the report space audit dto
     * @return the r
     */
    @PostMapping("/send/drawingAudit/msg")
    public R<Boolean> sendDrawingAuditMsg(@RequestBody ReportSpaceAuditDTO reportSpaceAuditDTO) {
        log.info("sendDrawingAuditMsg - 发送图纸审核通过信息, reportSpaceAuditDTO={}", reportSpaceAuditDTO);
        try {
            return R.successDef(reportSpaceService.sendDrawingAuditMsg(reportSpaceAuditDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

}
